using Microsoft.EntityFrameworkCore;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Config;
using net_work.Data.DTO.Cou;
using net_work.Data.Entity.Cou;
using net_work.Data.VO.Cou;
using net_work.IRepository.Cou;

namespace net_work.Repository.Cou;

public class CourseRepository(ApplicationDbContext dbContext) : Repository<Course>(dbContext), ICourseRepository
{
    public async Task<long> InsertAsync(CourseAddRequest courseAddRequest)
    {
        var existingCourse =
            await dbContext.Courses.IgnoreQueryFilters()
                .FirstOrDefaultAsync(c => c.CouCode == courseAddRequest.CouCode);
        if (existingCourse != null)
        {
            return -1;
        }

        var newCourse = new Course
        {
            CouCode = courseAddRequest.CouCode,
            CouName = courseAddRequest.CouName,
            CouDesc = courseAddRequest.CouDesc,
            CouProp = courseAddRequest.CouProp,
            CollegeId = courseAddRequest.CollegeId,
            Remark = courseAddRequest.Remark
        };
        await dbContext.Courses.AddAsync(newCourse);
        await dbContext.SaveChangesAsync();
        return newCourse.Id;
    }

    public async Task<bool> UpdateAsync(CourseUpdateRequest courseUpdateRequest)
    {
        var courses = await dbContext.Courses
            .IgnoreQueryFilters()
            .Where(c => c.CouCode == courseUpdateRequest.CouCode || c.Id == courseUpdateRequest.Id)
            .ToListAsync();

        var conflictCourse = courses.FirstOrDefault(x => x.Id != courseUpdateRequest.Id);
        if (conflictCourse != null)
        {
            return false;
        }

        var existingCourse = courses.FirstOrDefault(x => x.Id == courseUpdateRequest.Id);
        if (existingCourse == null)
        {
            return false;
        }

        existingCourse.CouCode = courseUpdateRequest.CouCode;
        existingCourse.CouName = courseUpdateRequest.CouName;
        existingCourse.CouDesc = courseUpdateRequest.CouDesc;
        existingCourse.CouProp = courseUpdateRequest.CouProp;
        existingCourse.CollegeId = courseUpdateRequest.CollegeId;
        existingCourse.Remark = courseUpdateRequest.Remark;

        await dbContext.SaveChangesAsync();
        return true;
    }

    public async Task<PagedResult<CourseResult>> GetCoursesPagedAsync(PagedRequest request)
    {
        var query = from course in dbContext.Courses
            join college in dbContext.Colleges on course.CollegeId equals college.Id
            select new CourseResult
            {
                Id = course.Id,
                CouCode = course.CouCode,
                CouName = course.CouName,
                CouDesc = course.CouDesc,
                CouProp = course.CouProp,
                CollegeId = course.CollegeId,
                CollegeName = college.ColleName,
                Remark = course.Remark,
                UpdatedTime = course.UpdatedTime,
                UpdatedBy = course.UpdatedBy,
                CreatedTime = course.CreatedTime,
                CreatedBy = course.CreatedBy,
                DelFlag = course.DelFlag
            };

        return await GetPagedQueryResultAsync(query, request);
    }
}